﻿using System;
using System.Collections.Generic;
using System.Text;
using AuthenticationServer.Data.Identity.Entities;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

//using AuthenticationServer.Models;

using System.Reflection;
using AuthenticationServer.Data.Identity.Configuration;

namespace AuthenticationServer.Data
{
    //public class AuthDbContext : IdentityDbContext
    //{
    //    public AuthDbContext(DbContextOptions<AuthDbContext> options)
    //        : base(options)
    //    {
    //    }
    //}
    public class AuthDbContext : IdentityDbContext<User,
        Role, string,
        UserClaim, UserRole, UserLogin,
        RoleClaim, UserToken>
    {
        public AuthDbContext(DbContextOptions<AuthDbContext> options)
            : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
            //builder.ApplyConfiguration

            //typeof(AuthDbContext).GetTypeInfo().Assembly
            //var typesToRegister = typeof(AuthDbContext).GetTypeInfo().Assembly.GetTypes()
            //   .Where(type => !String.IsNullOrEmpty(type.Namespace))
            //   .Where(type => type.BaseType != null && type.BaseType.IsGenericType &&
            //       type.BaseType.GetGenericTypeDefinition() == typeof(BaseEntityTypeConfiguration<>));
            //foreach (var type in typesToRegister)
            //{
            //    dynamic configurationInstance = Activator.CreateInstance(type);
            //    builder.ApplyConfiguration(configurationInstance);
            //}
            ApplyConfigurationsEntities(builder);
        }

        private static void ApplyConfigurationsEntities(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new RoleConfiguration());
            modelBuilder.ApplyConfiguration(new RoleClaimConfiguration());

            modelBuilder.ApplyConfiguration(new UserConfiguration());
            modelBuilder.ApplyConfiguration(new UserClaimConfiguration());
            modelBuilder.ApplyConfiguration(new UserTokenConfiguration());
            modelBuilder.ApplyConfiguration(new UserRoleConfiguration());
            modelBuilder.ApplyConfiguration(new UserLoginConfiguration());

        }
    }
}
